﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MBSProcessComponent;
namespace MBS.UserControls.PaymentUserControl
{
    public partial class AddBookingUserControl : System.Web.UI.UserControl
    {
        MBSBLC.BLC bookingblc = new MBSBLC.BLC();
        private bool result;
        private int bookID;
        public int BookID
        {
            get { return bookID; }
            set { bookID = value; }
        }
        public delegate void BookingAddHandler();
        public event BookingAddHandler BookingAddComplete;
        public event BookingAddHandler BookingAddFailed;
        public bool Result
        {
            get { return result; }
            set { result = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               bookingblc= new MBSBLC.BLC();
            }  
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
          //  Session["seatnumbers"] = "133";
          //  Session["showtime"] = 12;
          //  Session["userID"]=234;
            MovieBookingSession mbs = (MovieBookingSession)Session["MBS"];
            List<String> strList=new List<String>();
            strList=mbs.SelectedSeatsList;
            string seats="";
            foreach(string seat in strList)
            {
                if(string.Equals(seats,""))
                {
                    seats=seat;
                }
                else
                {
                     seats=seats+","+seat;
                }
               
            }
            int BookingID =  bookingblc.AddBooking(DateTime.Now,seats,mbs.ShowTimeId,mbs.UserID);
           
            if (BookingID >0)
            {
                mbs.BookingId = BookingID;
                Session["BOOKING_ID"] = BookingID;
               BookingAddComplete();
            //   BookID = bookingblc.getBookingID(mbs.ShowTimeId,mbs.UserID);
               string PagePath = "~/Ticket.aspx?bookID=" + BookingID;
               //Page.ClientScript.RegisterStartupScript(this.GetType(),"windowopen","javascript:window.open('~/Ticket.aspx?bookID='"+BookID+");",true);
               //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "windowopen", "javascript:alert('" + PagePath + "')", true);
               Response.Redirect(PagePath);
            }     
            else
            {
                MBSCache.releaseBlockedSeats(mbs);
                BookingAddFailed();
            }

        }
    }
}